package com.rs.controller.Rs;

import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.rs.model.CommonResult;
import com.rs.model.Document;
import com.rs.service.rs.DocumentService;
import com.rs.util.FileOperationsUtil;
import com.rs.util.ImagePanel;
import com.rs.util.SpringContextUtils;

public class DocumentJFame extends JInternalFrame {

	private static final long serialVersionUID = 1L;

	public DocumentJFame() {
		//设置是否允许自由调整大小
		setResizable(true);

		//设置是否提供“最大化”按钮
		setMaximizable(true);

		//设置是否提供“最小化”按钮
		setIconifiable(true);

		//设置是否提供关闭按钮
		setClosable(true);
		getContentPane().setLayout(null);

		setVisible(true);
	}

	public void alert(String alert) {
		JOptionPane.showMessageDialog(this, alert);
	}

	/**********查询所有的文档记录*******/
	class DoucumenLook extends DocumentJFame {

		private static final long serialVersionUID = 1L;

		private DocumentService ds = SpringContextUtils.getBean(DocumentService.class);

		private Document document = new Document();

		private JPanel contentPane;

		private JTable table;

		// 表头（列名）
		private Object[] columnNames = { "ID", "上传人", "项目名称", "文档名称", "上传时间", "操作" };

		private DefaultTableModel dtable;

		private JLabel show;//显示分页

		private JTextField prname;//项目名字

		private JTextField doname;//文档名字

		private Integer pageNum;//当前页

		private Integer pages;//总页数

		public DoucumenLook() {
			super();
			//设置标题
			setTitle("项目记录");
			//设置是否允许自由调整大小
			setResizable(false);
			//设置最大化
			setMaximizable(false);
			//设置大小
			setBounds(40, 35, 800, 550);
			//主体
			contentPane = new ImagePanel(800, 550, "twwe2.jpg");
			contentPane.setLayout(null);
			setContentPane(contentPane);

			//创建1个字体实例
			Font font = new Font("宋体", Font.BOLD, 14);

			//初始化当前页数为
			document.setPageNum(1);
			//查询
			CommonResult<Map<String, Object>> result = ds.doucumenConditionsPc(document);

			//为当前页赋值
			if (Optional.ofNullable(result.getData().get("pageNum")).isPresent()) {

				pageNum = (Integer) result.getData().get("pageNum");
			} else {
				pageNum = 1;
			}
			//总条数
			//total = (Integer) users.getData().get("total");
			//总页数
			if (Optional.ofNullable(result.getData().get("pages")).isPresent()) {
				pages = (Integer) result.getData().get("pages");
			} else {
				pages = 1;
			}

			// 创建一个表格，指定 所有行数据 和 表头
			dtable = new DefaultTableModel(getObjectArys(result), columnNames);

			table = new JTable(dtable) {
				private static final long serialVersionUID = 1L;

				//设置表格为不可编辑
				@Override
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			};
			//设置行高
			table.setRowHeight(25);
			table.setFont(font);
			table.getTableHeader().setBounds(20, 90, 750, 25);
			table.setBounds(20, 120, 750, 300);
			//表头不可拖动
			table.getTableHeader().setReorderingAllowed(false);
			//设置是否可以选择此模型中的行。 
			table.setRowSelectionAllowed(true);
			//设置表格为不可编辑
			//table.isCellEditable(uList.size(), 6);
			//设置只能选择一行
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			// 把 表头 添加到容器顶部（使用普通的中间容器添加表格时，表头 和 内容 需要分开添加）
			contentPane.add(table.getTableHeader(), null);
			// 把 表格内容 添加到容器中心
			contentPane.add(table, null);

			//表格的点击事件

			table.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						// 处理鼠标双击
						int row = table.getSelectedRow();
						String pId = table.getValueAt(row, 0).toString(); //得到所在行的第一个列的值，作为下面事件传递的参数
						Document doc = ds.documenDownloadPc(Integer.valueOf(pId)).getData();

						Frame frame = new Frame("文件对话框");
						FileDialog fd2 = new FileDialog(frame, "选择路径", FileDialog.SAVE);
						//将此文件对话框窗口的选定文件设置为指定的文件
						fd2.setFile(doc.getDo_name());
						fd2.setVisible(true);

						boolean flag = FileOperationsUtil.downloadFile(doc, fd2.getDirectory());
						if (flag) {
							alert("下载成功");
						} else {
							alert("下载失败");
						}

					}
				}

			});

			//分页

			show = new JLabel();
			show.setText("当前页：" + pageNum + " / 总页数：" + pages);
			show.setFont(new Font("宋体", Font.BOLD, 15));
			show.setBounds(300, 420, 200, 30);
			contentPane.add(show, null);

			final JButton fore = new JButton("首  页");
			fore.setBounds(100, 475, 80, 28);
			fore.setFont(font);
			contentPane.add(fore, null);
			fore.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = fore.getText();
					paging(type);

				}
			});

			final JButton previous = new JButton("上一页");
			previous.setBounds(280, 475, 80, 28);
			previous.setFont(font);
			contentPane.add(previous, null);
			previous.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = previous.getText();
					paging(type);
				}
			});

			final JButton later = new JButton("下一页");
			later.setBounds(470, 475, 80, 28);
			later.setFont(font);
			contentPane.add(later, null);
			later.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = later.getText();
					paging(type);

				}
			});

			final JButton aft = new JButton("尾  页");
			aft.setBounds(630, 475, 80, 28);
			aft.setFont(font);
			contentPane.add(aft, null);
			aft.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = aft.getText();
					paging(type);

				}
			});

			//条件查询
			prname = new JTextField("项目名", 10);
			prname.setBounds(130, 10, 90, 20);
			prname.setFont(font);
			contentPane.add(prname, null);

			doname = new JTextField("文档名", 10);
			doname.setBounds(20, 10, 90, 20);
			doname.setFont(font);
			contentPane.add(doname, null);

			//查询按钮
			JButton query = new JButton("查询");
			query.setBounds(590, 10, 70, 20);
			query.setFont(font);
			contentPane.add(query);
			query.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					//姓名
					if (!prname.getText().equals("项目名")) {

						document.setDo_prname(prname.getText());
					}
					//部门id
					if (!doname.getText().equals("文档名")) {

						document.setDo_name(doname.getText());

					}

					//查询
					//设置当前页为1
					document.setPageNum(1);
					CommonResult<Map<String, Object>> result = ds.doucumenConditionsPc(document);
					Object[][] shouye = getObjectArys(result);
					//使用数组 dataVector的值替换 dataVector实例变量中的值。 
					dtable.setDataVector(shouye, columnNames);
					table.setModel(dtable);
					pageNum = (Integer) result.getData().get("pageNum");
					pages = (Integer) result.getData().get("pages");
					show.setText("当前页：" + pageNum + " / 总页数：" + pages);

				}
			});

			//重置按钮
			JButton reset = new JButton("重置");
			reset.setBounds(680, 10, 70, 20);
			reset.setFont(font);
			contentPane.add(reset);
			reset.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					//重置
					reset();
				}
			});

		}

		public Object[][] getObjectArys(CommonResult<Map<String, Object>> documents) {
			@SuppressWarnings("unchecked")
			List<Document> dList = (List<Document>) documents.getData().get("list");

			Object[][] rowData = new Object[dList.size()][];
			for (int i = 0, n = dList.size(); i < n; i++) {
				Document document = dList.get(i);
				rowData[i] = document.toObjects(columnNames.length);
			}
			return rowData;
		}

		public void paging(String type) {
			switch (type) {
			case "首  页":
				document.setPageNum(1);
				CommonResult<Map<String, Object>> ufore = ds.doucumenConditionsPc(document);
				Object[][] shouye = getObjectArys(ufore);
				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(shouye, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) ufore.getData().get("pageNum");
				pages = (Integer) ufore.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				break;
			case "上一页":
				//当前页减1
				int previousNum = pageNum - 1;
				if (previousNum < 1) {
					alert("当前已是首页");
					previousNum = 1;
				}
				document.setPageNum(previousNum);
				CommonResult<Map<String, Object>> upreviou = ds.doucumenConditionsPc(document);
				Object[][] shangyi = getObjectArys(upreviou);

				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(shangyi, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) upreviou.getData().get("pageNum");
				pages = (Integer) upreviou.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);

				break;
			case "下一页":
				//当前页加1
				int laterNum = pageNum + 1;

				if (laterNum > pages) {
					alert("当前已是尾页");
					laterNum = pages;
				}

				document.setPageNum(laterNum);
				CommonResult<Map<String, Object>> ulater = ds.doucumenConditionsPc(document);

				Object[][] xiayiye = getObjectArys(ulater);

				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(xiayiye, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) ulater.getData().get("pageNum");
				pages = (Integer) ulater.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				break;
			case "尾  页":
				document.setPageNum(pages);
				CommonResult<Map<String, Object>> uaft = ds.doucumenConditionsPc(document);
				Object[][] weiye = getObjectArys(uaft);
				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(weiye, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) uaft.getData().get("pageNum");
				pages = (Integer) uaft.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				break;
			}
		}

		//重置
		public void reset() {

			prname.setText("项目名");

			doname.setText("文档名");

			document.setDo_prname(null);

			document.setDo_name(null);

		}

	}

}
